package reflect;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;

/**
 * 使用指定的构造器进行实例化操作
 */
public class ReflectDemo3 {
    public static void main(String[] args) throws ClassNotFoundException, NoSuchMethodException, InvocationTargetException, InstantiationException, IllegalAccessException {
        Person p = new Person("李四");
        System.out.println(p);

        Class cls = Class.forName("reflect.Person");
        //Person(String name)
        Constructor c = cls.getConstructor(String.class);
        Object o = c.newInstance("王五");
        System.out.println(o);


        //Person(String name,int age)
        Constructor c2 = cls.getConstructor(String.class,int.class);
        o = c2.newInstance("赵六",33);
        System.out.println(o);

    }
}
